package com.eorchis.module.remind.service.impl;

import java.text.MessageFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.eorchis.constants.RoleConstants;
import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.service.impl.AbstractBaseService;
import com.eorchis.module.basedata.domain.BaseData;
import com.eorchis.module.basedata.service.cache.BaseDataCacheUtil;
import com.eorchis.module.remind.dao.IRemindDao;
import com.eorchis.module.remind.domain.Remind;
import com.eorchis.module.remind.domain.RemindUser;
import com.eorchis.module.remind.service.IRemindService;
import com.eorchis.module.remind.ui.commond.RemindQueryCommond;
import com.eorchis.module.remind.ui.commond.RemindValidCommond;
import com.eorchis.module.reminduserlink.dao.IRemindUserLinkDao;
import com.eorchis.module.reminduserlink.domain.RemindUserLink;
import com.eorchis.module.reminduserlink.service.IRemindUserLinkService;
import com.eorchis.module.trainingclass.querybean.TrainingClassQueryBean;
import com.eorchis.module.util.DateUtil;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: RemindServiceImpl<br>
 * Description: 提醒表管理业务接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author RongSL
 * @createDate 2016-05-06
 * @version $Revision$
 */
// 声明本类为Server实现类，声明bean名称
@Service(value = "com.eorchis.module.remind.service.impl.RemindServiceImpl")
public class RemindServiceImpl extends AbstractBaseService implements IRemindService {

	@Resource(name = "com.eorchis.module.remind.dao.impl.RemindDaoImpl")
	private IRemindDao remindDao;

	@Resource(name = "com.eorchis.module.basedata.service.cache.BaseDataCacheUtil")
	private BaseDataCacheUtil baseDataCacheUtil;
	
	// 提醒用户关联表接口
	@Resource(name = "com.eorchis.module.reminduserlink.service.impl.RemindUserLinkServiceImpl")
	private IRemindUserLinkService remindUserLinkService;
	
	@Override
	public IDaoSupport getDaoSupport() {
		return remindDao;
	}

	@Resource(name = "com.eorchis.module.reminduserlink.dao.impl.RemindUserLinkDaoImpl")
	private IRemindUserLinkDao remindUserLinkDao;
	
	@Override
	@SuppressWarnings("unchecked")
	public RemindValidCommond toCommond(IBaseEntity entity) {
		return new RemindValidCommond((Remind)entity);
	}
	
	DateUtil d=new DateUtil();
	
	/**
	 * @author RongSL
	 * @Date 2016-05-10
	 * @param parameter[0]  提醒类型
	 * Description 提醒统一调用接口
	 * */
	public void remindOperate(String... parameter) throws Exception{
		try {
			if(PropertyUtil.objectNotEmpty(parameter)){
				Map<String, BaseData> baseDataMap =  baseDataCacheUtil.getBaseData();
//				BaseData bd=baseDataMap.get(parameter[0]);
				RemindQueryCommond commond=new RemindQueryCommond();
				commond.setSearchBaseDateCode(parameter[0]);
				BaseData bd=remindDao.findBaseDate(commond);
				Object o[] = parameter;
				//如果提醒分类被关闭，则不推送提醒
				if(bd.getActiveState()==2){
					return;
				}
				Set<RemindUserLink> RemindUserList = new HashSet<RemindUserLink>();
				Remind rind=new Remind();
				RemindUserLink rul=null;
				String remind=null;
				RemindQueryCommond query=new RemindQueryCommond();
				
				if(parameter[0].equals(Remind.XZPXB)){
					query.setScopesDepNames(parameter[2]);
					query.setRoleCode(RoleConstants.ROLE_BW_NAME);
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.XZPXB+"TX"), o);
				}
				if(parameter[0].equals(Remind.WSPXB)){
					if(parameter[2].equals(RoleConstants.ROLE_ZZB_NAME)){		//向中组部管理员推送
						query.setRoleCode(RoleConstants.ROLE_ZZB_NAME);
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.WSPXB+"ZZBTX"), o);
					}else{		//向地方管理员推送
						query.setRoleCode(RoleConstants.ROLE_DF_NAME);
						query.setScopesDepNames(parameter[4]);
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.WSPXB+"DFTX"), o);
					}
				}
				if(parameter[0].equals(Remind.FBPXB)){
					if(parameter[2].equals(RoleConstants.ROLE_ZZB_NAME)){		//向中组部管理员推送
						query.setRoleCode(RoleConstants.ROLE_ZZB_NAME);
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.FBPXB+"ZZBTX"), o);
					}else{		//向地方管理员推送
						query.setRoleCode(RoleConstants.ROLE_DF_NAME);
						query.setScopesDepNames(parameter[4]);
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.FBPXB+"DFTX"), o);
					}
				}
				if(parameter[0].equals(Remind.CXFBPXB)){
					if(parameter[2].equals(RoleConstants.ROLE_ZZB_NAME)){		//向中组部管理员推送
						query.setRoleCode(RoleConstants.ROLE_ZZB_NAME);
						String par=PropertyUtil.objectNotEmpty(parameter[4].trim())?"("+parameter[4]+")":"";
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.CXFBPXB+"ZZBTX"), o)+"\n"+par;
					}else{		//向地方管理员推送
						query.setRoleCode(RoleConstants.ROLE_DF_NAME);
						query.setScopesDepNames(parameter[4]);
						String par=PropertyUtil.objectNotEmpty(parameter[5].trim())?"("+parameter[5]+")":"";
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.CXFBPXB+"DFTX"), o)+"\n"+par;
					}
				}
				if(parameter[0].equals(Remind.KB)){
					query.setRoleCode(parameter[2]);
					if(parameter[2].equals(RoleConstants.ROLE_DF_NAME)){
						query.setScopesDepNames(parameter[3]);
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.KB+"DFTX"), o);
					}else if(parameter[2].equals(RoleConstants.ROLE_BW_NAME)){
						query.setScopesDepNames(parameter[3]);
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.KB+"BWTX"), o);
					}else{
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.KB+"ZZBTX"), o);
					}
				}
				if(parameter[0].equals(Remind.TZME)){
					if(parameter[4].equals("ZZB")){
						query.setRoleCode(RoleConstants.ROLE_ZZB_NAME);
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.TZME+"ZZBTX"), o);
					}else{
						query.setScopesDepNames(parameter[3]);
						query.setRoleCode(RoleConstants.ROLE_DF_NAME);
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.TZME+"DFTX"), o);
					}
				}
				if(parameter[0].equals(Remind.XYBM)){
					query.setRoleCode(RoleConstants.ROLE_BW_NAME);
					query.setScopesDepNames(parameter[4]);
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.XYBM+"TX"), o);
				}
				if(parameter[0].equals(Remind.CXBMSQ)){
					query.setRoleCode(RoleConstants.ROLE_BW_NAME);
					query.setScopesDepNames(parameter[4]);
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.CXBMSQ+"TX"), o);
				}
				if(parameter[0].equals(Remind.BMSH)){
					query.setRoleCode(RoleConstants.ROLE_DF_NAME);
					query.setScopesDepNames(parameter[5]);
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.BMSH+"TX"), o);
				}
				if(parameter[0].equals(Remind.DBM)){
					query.setRoleCode(RoleConstants.ROLE_DF_NAME);
					query.setScopesDepNames(parameter[3]);
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.DBM+"TX"), o);
				}
				if(parameter[0].equals(Remind.BMKS)){
					query.setScopesDepIDs(parameter[2]);
//					if(parameter[2].equals(RoleConstants.ROLE_DF_NAME)){
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.BMKS+"DFTX"), o);
//					}else if(parameter[2].equals(RoleConstants.ROLE_BW_NAME)){
//						role.setScopesDepNames(parameter[3]);
//						remind=MessageFormat.format(remindDao.findRemindDate(Remind.BMKS+"BWTX"), o);
//					}
				}
				if(parameter[0].equals(Remind.BMJS)){
					if(parameter[2].equals(RoleConstants.ROLE_DF_NAME)){
						query.setRoleCode(RoleConstants.ROLE_DF_NAME);
						query.setScopesDepNames(parameter[3]);
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.BMJS+"DFTX"), o);
					}else{
						query.setRoleCode(RoleConstants.ROLE_BW_NAME);
						query.setScopesDepNames(parameter[3]);
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.BMJS+"BWTX"), o);
					}
				}
				if(parameter[0].equals(Remind.XYPJ)){
					query.setRoleCode(RoleConstants.ROLE_BW_NAME);
					query.setScopesDepNames(parameter[3]);
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.XYPJ+"TX"), o);
				}
				if(parameter[0].equals(Remind.XYPJJS)){
					query.setRoleCode(RoleConstants.ROLE_BW_NAME);
					query.setScopesDepNames(parameter[3]);
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.XYPJJS+"TX"), o);
				}
				if(parameter[0].equals(Remind.PGKS)){
					query.setRoleCode(RoleConstants.ROLE_BW_NAME);
					query.setScopesDepNames(parameter[3]);
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.PGKS+"TX"), o);
				}
				if(parameter[0].equals(Remind.YQH)){
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.YQH+"TX"), o);
					query.setRoleCode(RoleConstants.ROLE_BW_NAME);
					query.setScopesDepNames(parameter[3]);
				}
				
				if(parameter[0].equals(Remind.TJPXJH)){
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.TJPXJH+"TX"), o);
						query.setRoleCode(RoleConstants.ROLE_ZZB_NAME);
				}
				if(parameter[0].equals(Remind.SHPXJH)){
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.SHPXJH+"TX"), o);
					query.setRoleCode(RoleConstants.ROLE_BW_NAME);
					query.setScopesDepNames(parameter[3]);
				}
				if(parameter[0].equals(Remind.XFPXJH)){
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.XFPXJH+"TX"), o);
					query.setRoleCode(RoleConstants.ROLE_DF_NAME);
					query.setScopesDepNames(parameter[2]);
				}
				if(parameter[0].equals(Remind.TJYJ)){
					if(parameter[3].equals(RoleConstants.ROLE_ZZB_NAME)){
					remind=MessageFormat.format(remindDao.findRemindDate(Remind.TJYJ+"ZZBTX"), o);
					query.setRoleCode(RoleConstants.ROLE_ZZB_NAME);
					}
				}
				if(parameter[0].equals(Remind.FBPXJH)){
					if(parameter[3].equals(RoleConstants.ROLE_BW_NAME)){
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.FBPXJH+"BWTX"), o);
						query.setRoleCode(RoleConstants.ROLE_BW_NAME);
						query.setScopesDepNames(parameter[1]);
					}else{
						remind=MessageFormat.format(remindDao.findRemindDate(Remind.FBPXJH+"DFTX"), o);
						query.setRoleCode(RoleConstants.ROLE_DF_NAME);
						query.setScopesDepNames(parameter[1]);
					}
				}
//				if(parameter[0].equals(Remind.GXTZME)){
//					remind=MessageFormat.format(remindDao.findRemindDate(Remind.FBPXJH+"TX"), o);
//					role.setAdmins(RoleConstants.ROLE_BW_NAME);
//					role.setScopesDepNames(parameter[3]);
//				}

				List<RemindUser> list=remindDao.findAdministrator(query);
				//删选预发送提醒人员
				for(String  type:Remind.NoSparkRemind){
					if(type.equals(parameter[0])){
						for(int i=0;i<list.size();i++){
							RemindUser r=list.get(i);
							if(!remindUserLinkService.seePermitState(parameter[parameter.length-2]+parameter[0]+r.getUserId())){
								list.remove(i);
								i=-1;
							}
						}
						break;
					}
				}
				
				if(PropertyUtil.objectNotEmpty(list)){
					//新增提醒
					rind.setRemindValue(remind);
					rind.setActiveState(1);
					rind.setCreateDate(new Date());
					rind.setRemindType(bd);
					//最后一个参数为培训班分类
					rind.setRemindCategory(baseDataMap.get(parameter[parameter.length-1]));
					rind.setRemindState(1);
					rind.setRemindUserList(RemindUserList);
					remindDao.save(rind);
					
					//新增提醒关联
					for(int i=0;i<list.size();i++){
						RemindUser r=list.get(i);
						rul=new RemindUserLink();
						rul.setActiveState(1);
						rul.setCreateDate(new Date());
						rul.setLookState(RemindUserLink.LOOK_STATE_NO);
						rul.setRemind(rind);
						rul.setRemindValue(remind);
						rul.setReplyUserId(r.getUserId());
						rul.setRemindClassIdCode(parameter[parameter.length-2]+parameter[0]+r.getUserId());
						rul.setSendPermit(RemindUserLink.SEND_PERMIT_NO);
						remindUserLinkDao.save(rul);
						RemindUserList.add(rul);
						//更新相关提醒状态为不可发送
						for(String  type:Remind.NoSparkRemind){
							if(parameter[0].equals(type)){
								remindUserLinkService.updateSendRemindStateNo(parameter[parameter.length-2]+parameter[0]+r.getUserId());
							}
						}
					}
					
					rind.setRemindUserList(RemindUserList);
					//更新提醒关联
					remindDao.update(rind);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("接口错误！");
		}
	}
	
	/**
	 * 无触发提醒
	 * @author RongSL
	 * @throws Exception 
	 * @createDate 2016-05-13
	 **/
	@SuppressWarnings("static-access")
	public void NoSparkRemind(String str) throws Exception{
		String rd=remindDao.findRemindDate(str);
		List<TrainingClassQueryBean> queryBean;
		if(str.equals(Remind.KB)){
			//查询5天后开班的班级	
			String deptName[];
			RemindQueryCommond commond=new RemindQueryCommond();
			commond.setKb(Integer.valueOf(rd));
			queryBean=remindDao.findNClass(commond);
			for(TrainingClassQueryBean bean:queryBean){
				if(PropertyUtil.objectNotEmpty(bean.getAsignCountCount())){
					deptName=bean.getTrainingDeptName().split(",");
					//给地方管理员推送提醒
					for(int i=0;i<(deptName.length);i++){
						remindOperate(Remind.KB,bean.getClassName(),RoleConstants.ROLE_DF_NAME,deptName[i],bean.getActualDate()+"",bean.getClassId(),Remind.PXB);
					}
				}
				//给部委管理员推送提醒
				remindOperate(Remind.KB,bean.getClassName(),RoleConstants.ROLE_BW_NAME,bean.getMinistrieName(),bean.getActualDate()+"",bean.getClassId(),Remind.PXB);
				//给中组部管理推送提醒
				remindOperate(Remind.KB,bean.getClassName(),RoleConstants.ROLE_ZZB_NAME,bean.getActualDate()+"",bean.getClassId(),Remind.PXB);
			}
		}
		if(str.equals(Remind.BMKS)){
			//查询当开班的班级
			String[] deptIDS;
			RemindQueryCommond commond=new RemindQueryCommond();
			commond.setBmks(Integer.valueOf(rd));
			queryBean=remindDao.findNClass(commond);
			for(TrainingClassQueryBean bean:queryBean){
				if(PropertyUtil.objectNotEmpty(bean.getDepIDs())){
					deptIDS=bean.getDepIDs().split(",");
					//给各报名单位推送提醒
					for(int i=0;i<deptIDS.length;i++){
						remindOperate(Remind.BMKS,bean.getClassName(),deptIDS[i],d.StringDateToStr(bean.getEnterBeginTime()),d.StringDateToStr(bean.getEnterEndTime()),bean.getClassId(),Remind.PXB);
					}
				}
			}
		}
		if(str.equals(Remind.BMJS)){
			//查询报名截止前一天的班级
			String[] deptName;
			Integer deptNum;
			String[] asignCount;
			String  deptName2;
			Integer  deptNum2;
			RemindQueryCommond commond=new RemindQueryCommond();
			List<TrainingClassQueryBean> list;
			commond.setBmjs(Integer.valueOf(rd));
			queryBean=remindDao.findNClass(commond);
			for(TrainingClassQueryBean bean:queryBean){
				deptName=bean.getTrainingDeptName().split(",");
				asignCount=bean.getAsignCountCount().split(",");
				if(PropertyUtil.objectNotEmpty(asignCount)){
					for(int i=0;i<deptName.length;i++){
						commond.setSearchClassIds(bean.getClassId());
						list=remindDao.findSignClass(commond);
						for(TrainingClassQueryBean c:list){
							deptName2=c.getDeptName();
							deptNum2=c.getDepUserNum();
							if(deptName[i].equals(deptName2)){
								deptNum=Integer.valueOf(asignCount[i].substring(asignCount[i].lastIndexOf(":")+1));
								if(deptNum>deptNum2){
									//给地方管理员推送提醒
									remindOperate(Remind.BMJS,bean.getClassName(),RoleConstants.ROLE_DF_NAME,deptName2,d.StringDateToStr(bean.getEnterEndTime()),bean.getClassId(),Remind.PXB);
									//给部委管理员推送提醒
									remindOperate(Remind.BMJS,bean.getClassName(),RoleConstants.ROLE_BW_NAME,bean.getMinistrieName(),d.StringDateToStr(bean.getEnterEndTime()),deptName2,bean.getClassId(),Remind.PXB);
								}
							}
						}
					}
				}
			}
		}
		if(str.equals(Remind.XYPJ)){
			//培训班结束2天后的班级
			RemindQueryCommond commond=new RemindQueryCommond();
			commond.setXypj(Integer.valueOf(rd));
			queryBean=remindDao.findNClass(commond);
			for(TrainingClassQueryBean bean:queryBean){
				//给部委管理员推送提醒
				remindOperate(Remind.XYPJ,bean.getClassName(),RoleConstants.ROLE_BW_NAME,bean.getMinistrieName(),bean.getClassId(),Remind.PXB);
			}
		}
		if(str.equals(Remind.PGKS)){
			//距评估开始只有一天的班级
			RemindQueryCommond commond=new RemindQueryCommond();
			commond.setPgks(Integer.valueOf(rd));
			queryBean=remindDao.findNClass(commond);
			for(TrainingClassQueryBean bean:queryBean){
				if(PropertyUtil.objectNotEmpty(bean.getQuestionnaireName())){
					//给部委管理员推送提醒
					remindOperate(Remind.PGKS,bean.getClassName(),RoleConstants.ROLE_BW_NAME,bean.getMinistrieName(),d.StringDateToStr(bean.getAssessStartTime()),d.StringDateToStr(bean.getAssessEndTime()),bean.getQuestionnaireName(),bean.getClassId(),Remind.PXB);
				}
			}
		}
		if(str.equals(Remind.YQH)){
			//查询发邀请函班级
			RemindQueryCommond commond=new RemindQueryCommond();
			commond.setYqh(Integer.valueOf(rd));
			queryBean=remindDao.findNClass(commond);
			for(TrainingClassQueryBean bean:queryBean){
				//给部委管理员推送提醒
				remindOperate(Remind.YQH,bean.getClassName(),RoleConstants.ROLE_BW_NAME,bean.getMinistrieName(),d.StringDateToStr(bean.getEnterBeginTime()),d.StringDateToStr(bean.getEnterEndTime()),bean.getClassId(),Remind.PXB);
			}
		}
	}
	
	/**
	 * 查询提醒列表
	 * @Date 2016-06-12
	 * @author RongSL
	 * */
	public List<RemindValidCommond> findRemindList(RemindQueryCommond queryCommond)throws Exception{
		return remindDao.findRemindList(queryCommond);
	}
	
	/**
	 * 更改提醒状态
	 * @Date 2016-06-14
	 * @author RongSL
	 * */
	@Override
	public void updateRemindState(RemindValidCommond valid) throws Exception{
		remindDao.updateRemindState(valid);
	}
	
	/**
	 * 提醒类型查询
	 * @Date 2016-06-13
	 * @author RongSL
	 * */
	@Override
	public List<BaseData> findBaseDataList(RemindQueryCommond commond)throws Exception{
		return remindDao.findBaseDataList(commond);
	}

	/**
	 * 更改提醒类型状态
	 * @Date 2016-06-14
	 * @author RongSL
	 * */
	@Override
	public void updateRemindTypeState(RemindQueryCommond commond) throws Exception{
		remindDao.updateRemindTypeState(commond);
	}
	/**
	 * 查询无提醒时间
	 * @Date 2016-06-24
	 * @author RongSL
	 * */
	@Override
	public String findRemindData(String Code) throws Exception{
		return remindDao.findRemindDate(Code);
	}
	
	/**
	 * 更改消息发送的状态
	 * @Date 2016-06-14
	 * @author NingSS
	 * */
	@Override
	public void updateRemindTypeS(RemindQueryCommond commond) throws Exception{
		remindDao.updateRemindTypeS(commond);
	}
	
	/**
	 * 查询管理员
	 * @Date 2016-05-13
	 * @author RongSL
	 * */
	@Override
	public List<RemindUser> findAdministrator(RemindQueryCommond query)throws Exception{
		return remindDao.findAdministrator(query);
	}
	/**
	 * 查询普通学员
	 * @author RongSL
	 * 2018年1月3日
	 * */
	@Override
	public List<RemindUser> findUserList(RemindQueryCommond query)throws Exception{
		return remindDao.findUserList(query);
	}
}
